package base;

/**
 * @author aiowang
 * @description:
 * @date 2020/9/21 20:59
 */
public class SnchronizedTest {
    public static void main(String[] args) {

        class Demo {

            public synchronized void synMethod() {
                for (int i = 0; i < 100000; i++){
                    Math.atan2(1,1);
                    Math.hypot(1,1);
                }
            }

            public void synBlock() {
                synchronized (this) {
                    for (int i = 0; i < 100000; i++){
                        Math.atan2(1,1);
                        Math.hypot(1,1);
                    }
                }
            }
        }

        Demo demo = new Demo();
        long start, diff;
        start = System.currentTimeMillis();
        demo.synMethod();                                 // 调用“synchronized方法块”
        diff = System.currentTimeMillis() - start;        // 获取“时间差值”
        System.out.println("synMethod() : " + diff);


        start = System.currentTimeMillis();
        demo.synBlock();                                // 调用“synchronized方法块”
        diff = System.currentTimeMillis() - start;      // 获取“时间差值”
        System.out.println("synBlock()  : " + diff);
    }
}